<template>
  <div class="setting">
    <el-tabs v-model="activeName" @tab-click="">
      <el-tab-pane label="角色管理" name="first">
        <div class="bigbox row-padd">
          <el-button type="primary" size="small" @click="addclick"
            >+ 新建角色</el-button
          >
          <el-table
            :cell-style="{ 'text-align': 'center' }"
            :header-cell-style="{
              'text-align': 'center',
              backgroundColor: 'rgba(250, 250, 250)',
            }"
            :data="tableData"
            height="250"
            border
            style="width: 100%"
          >
            <el-table-column prop="date" type="index" label="序号" width="170">
            </el-table-column>
            <el-table-column prop="name" label="角色名" width="170">
            </el-table-column>
            <el-table-column prop="description" label="描述"> </el-table-column>
            <el-table-column prop="cao" label="操作" width="250">
              <template slot-scope="scope">
                <el-button
                  type="success"
                  size="small"
                  @click="givesetclick(scope.row.id)"
                  >分配权限</el-button
                >
                <el-button
                  type="primary"
                  @click="eaitclick(scope.row)"
                  size="small"
                  >修改</el-button
                >
                <el-button
                  type="danger"
                  @click="remveclick(scope.row)"
                  size="small"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page="currentPage"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
          </el-pagination>
        </div>
      </el-tab-pane>
      <el-tab-pane label="公司信息" name="secend">
        <div class="boxsing">
          <el-form ref="form" label-width="80px">
            <el-form-item label="企业名称">
              <el-input disabled v-model="companyinfo.name"></el-input>
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input
                disabled
                v-model="companyinfo.companyAddress"
              ></el-input>
            </el-form-item>
            <el-form-item label="公司电话">
              <el-input disabled v-model="companyinfo.companyPhone"></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input disabled v-model="companyinfo.mailbox"></el-input>
            </el-form-item>
            <el-form-item label="备注">
              <el-input disabled v-model="companyinfo.remarks"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-tab-pane>
    </el-tabs>
    <el-dialog :title="titlefun" :visible="digshow" @close="digshow = false">
      <el-form :model="form" ref="myfrom" :rules="rules">
        <el-form-item label="角色名" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button @click="subbit" type="primary">确 定</el-button>
      </div>
    </el-dialog>
    <setdig
      :setdigshow="setdigshow"
      @closedig="closeclick"
      ref="myrole"
    ></setdig>
  </div>
</template>

<script>
import {
  getdatelist,
  getcompantid,
  editRoleApi,
  addRoleApi,
  delRoleApi,
} from "../../api/setting";
import { mapGetters } from "vuex";
import setdig from "./setdig.vue";
export default {
  components: {
    setdig,
  },
  data() {
    return {
      setdigshow: false,
      digshow: false,
      activeName: "first",
      tableData: [],
      currentPage: 1,
      // 页容量选项
      pageSizes: [2, 3, 5],
      // 选中的页容量
      pageSize: 2,
      total: 0,
      companyinfo: {},
      formLabelWidth: "120px",
      form: {
        name: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["userinfo"]),
    titlefun() {
      return this.form.id ? "修改角色" : "增加角色";
    },
  },
  methods: {
    givesetclick(val) {
      this.$refs.myrole.getproplerole(val);
      this.setdigshow = true;
    },
    closeclick() {
      this.setdigshow = false;
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      // 重新发送请求到服务器
      this.getcomlist();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.getcomlist();
    },
    quxiao() {
      this.digshow = false;
      this.form = {
        name: "",
        description: "",
      };
    },
    addclick() {
      this.digshow = true;
    },
    eaitclick(val) {
      this.digshow = true;
      this.form = { ...val };
    },
    subbit() {
      this.$refs.myfrom.validate(async (isok) => {
        if (isok) {
          if (this.form.id) {
            await editRoleApi(this.form);
            try {
              this.$message({
                type: "success",
                message: "修改成功!",
              });
            } catch {}
          } else {
            await addRoleApi(this.form);
            try {
              this.$message({
                type: "success",
                message: "新增成功!",
              });
            } catch {}
          }
          this.digshow = false;
          this.form = {
            name: "",
            description: "",
          };
          this.getcomlist();
        }
      });
    },
    remveclick(val) {
      this.$confirm("是否删除该角色?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await delRoleApi(val.id);
          if (this.tableData.length <= 1 && this.currentPage > 1) {
            // 如果总数据长度小于 1，删除之后直接跳转到第一页
            this.currentPage = this.currentPage - 1;
          }
          this.getcomlist();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {});
    },
    async getcomp() {
      let res = await getcompantid(this.userinfo.companyId);
      console.log(res);
      this.companyinfo = res;
    },
    async getcomlist() {
      let res = await getdatelist({
        page: this.currentPage,
        pagesize: this.pageSize,
      });
      this.tableData = res.rows;
      this.total = res.total;
    },
  },
  async created() {
    console.log(this.tableData);
    this.getcomlist();
    this.getcomp();
  },
};
</script>

<style lang='scss'>
.setting {
  padding: 20px;
  background-color: white;
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  border: 1px solid #ebeef5;
  .row-padd {
    padding: 30px 50px 5px 40px;
  }
  .box {
    margin-bottom: 10px;
    padding: 5px 0;
    height: 1px;
    border: 1px solid transparent;
    border-bottom: 1px solid rgba(231, 231, 231);
  }
  .el-table {
    margin-top: 20px;
  }
  .el-pagination {
    margin-top: 10px;
    text-align: right;
  }
  .el-form {
    .el-input {
      width: 60%;
    }
  }
  .el-dialog__body {
    padding-bottom: 0;
  }
  .el-form {
    margin-left: 50px;
    .el-input__inner {
      width: 300px;
    }
  }
  .boxsing {
    margin-left: 280px;
  }
}
</style>